package com.jfinal.bbsapp.common.kit;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.log4j.Logger;
import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

public class CommonUtils {
	private static final Logger logger = Logger.getLogger(CommonUtils.class);
	
	@MyAnnotation(memo="从token中取userid")
	@SuppressWarnings("unchecked")
	public static String getuseridbytoken(String token) {
		String key = "", userid = "";
		try {
			Cache rds = Redis.use();
			Set<String> keys = rds.keys("app-" + token + "-*");
			if (keys.size() > 0)
				key = keys.iterator().next();
			if( !"".equals(key) && rds.exists(key)){
				Map<Object, Object> user = rds.hgetAll(key);
				userid = user.get("userid") == null ? "" : user.get("userid").toString();
			}
		} catch (Exception e) {
			logger.error("------Exception------", e);
		}
		return userid;
	}
	
	@MyAnnotation(memo="更新用户信息缓存")
	public static Boolean uptoken(String token) {
		try {
			String userid=CommonUtils.getuseridbytoken(token);
			Record rec = Db.findFirst("select cast(userid as char) userid,mobile,nickname,headimg,faceId,flag"
				+ " from c_user where userid=?",userid);
			rec.set("token", token);
			Map<Object, Object> data = Maps.newHashMap();
			for(String key : rec.getColumns().keySet()){
				data.put(key, rec.get(key));
			}

			Cache rds = Redis.use();
			rds.hmset("app-" + token + "-"  + rec.get("userid"), data);
			return true;
		} catch (Exception e) {
			logger.error("Exception:" , e);
			return false;	
		}
	}
	
	@MyAnnotation(memo="更新用户信息缓存通过ID")
	public static Boolean uptokenbyid(String userid) {
		try {
			Record rec = Db.findFirst("select cast(userid as char) userid,mobile,nickname,headimg,faceId,flag"
				+ " from c_user where userid=?",userid);
			String token = UUID.randomUUID().toString().replace("-", "");
			rec.set("token", token);
			Map<Object, Object> data = Maps.newHashMap();
			for(String key : rec.getColumns().keySet()){
				data.put(key, rec.get(key));
			}

			Cache rds = Redis.use();
			rds.hmset("app-" + token + "-"  + rec.get("userid"), data);
			return true;
		} catch (Exception e) {
			logger.error("Exception:" , e);
			return false;	
		}
	}
	
	@MyAnnotation(memo="删除用户信息缓存")
	public static Boolean upuser(String userid) {
		try {
			Cache rds = Redis.use();
			Set<String> keys = rds.keys("app-*-" + userid);
			if (keys.size() > 0) {
				for (String key : keys) {
					rds.del(key);
				}
			}
			return true;
		} catch (Exception e) {
			logger.error("Exception:" , e);
			return false;	
		}
	}

	@MyAnnotation(memo="处理分页参数")
	public static int getPageNum(String pageNum) {
		int pagenum = pageNum == null || "0".equals(pageNum) || "".equals(pageNum.trim()) ? 1
				: Integer.parseInt(pageNum);
		return pagenum;
	}

	public static int getPageSize(String pageSize) {
		int pagesize = pageSize == null || "".equals(pageSize.trim()) ? 10 : Integer.parseInt(pageSize);
		return pagesize;
	}
}
